<script setup lang="ts">
import PageSkeleton from './components/PageSkeleton.vue'
import { getCategoryTop } from '@/services/category'
import { getHomeBanner } from '@/services/home'
import type { CategoryChildItem, CategoryTopItem } from '@/types/category'
import type { BannerItem } from '@/types/home'
import { onLoad } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'

const bannerList = ref<BannerItem[]>([])
const getBannerData = async () => {
  const res = await getHomeBanner({ distributionSite: 2 })
  bannerList.value = res.result
}

// 当前选中分类
const activeIndex = ref(0)
// 分类导航数据
const categoryList = ref<CategoryTopItem[]>([])
// 二级分类数据
const subCategoryList = computed(() => {
  return categoryList.value[activeIndex.value]?.children || []
})
// 是否数据加载完毕
const finish = ref(false)
// 获取分类数据
const getCategoryTopData = async () => {
  const res = await getCategoryTop()
  categoryList.value = res.result
}
onLoad(async () => {
  await Promise.all([getBannerData(), getCategoryTopData()])
  finish.value = true
})
</script>

<template>
  <view class="category">
    <!-- 搜索框 -->
    <view class="search">
      <view class="input">
        <text class="icon-search">女靴</text>
      </view>
    </view>
    <PageSkeleton v-if="!finish"></PageSkeleton>
    <view class="categories" v-else>
      <!-- 左边导航 -->
      <scroll-view class="primary" scroll-y>
        <view
          class="item"
          :class="{ active: activeIndex == index }"
          v-for="(item, index) in categoryList"
          :key="item.id"
          @tap="activeIndex = index"
        >
          <text class="name">{{ item.name }}</text>
        </view>
      </scroll-view>
      <!-- 右边 -->
      <scroll-view class="secondary" scroll-y>
        <!-- 轮播图 -->
        <XtxSwiper class="banner" :list="bannerList"></XtxSwiper>
        <!-- 内容区域 -->
        <view class="panel" v-for="item in subCategoryList" :key="item.id">
          <view class="title">
            <text class="name">{{ item.name }}</text>
            <navigator class="more" hover-class="none"
              >全部<text class="icon-right"></text
            ></navigator>
          </view>
          <view class="section">
            <navigator
              class="goods"
              :url="`/pages/goods/goods?id=${good.id}`"
              hover-class="none"
              v-for="good in item.goods"
              :key="good.id"
            >
              <image class="image" :src="good.picture"></image>
              <view class="name ellipsis">{{ good.name }}</view>
              <view class="price">
                <text class="symbol">¥</text>
                <text class="number">{{ good.price }}</text>
              </view>
            </navigator>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  overflow: hidden;
}
.category {
  height: 100%;
  display: flex;
  flex-direction: column;
}
// 搜索框
.search {
  padding: 20rpx 30rpx 20rpx;
  .input {
    width: 100%;
    border-radius: 30px;
    color: #8b8b8b;
    font-size: 28rpx;
    display: flex;
    align-items: center;
    height: 64rpx;
    padding-left: 26rpx;
    background-color: #f3f4f4;
    .icon-search {
      &::before {
        margin-right: 10rpx;
      }
    }
  }
}
// 分类
.categories {
  flex: 1;
  display: flex;
  min-height: 400rpx;
}
.primary {
  overflow: hidden;
  flex: none;
  width: 180rpx;
  height: 100%;
  background-color: #f6f6f6;
  .item {
    height: 96rpx;
    font-size: 26rpx;
    color: #595c63;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    &::after {
      content: '';
      position: absolute;
      left: 42rpx;
      bottom: 0;
      width: 96rpx;
      border-top: 1rpx solid #e3e4e7;
    }
    &:last-child::after {
      display: none;
    }
  }
  .active {
    background-color: #fff;
    &::before {
      content: '';
      position: absolute;
      left: 0;
      width: 8rpx;
      height: 100%;
      background-color: #27ba9b;
    }
    &::after {
      display: none;
    }
  }
}
.secondary {
  .carousel {
    height: 200rpx;
    margin: 0 30rpx 20rpx;
    border-radius: 4rpx;
    overflow: hidden;
  }
  .panel {
    margin: 0 30rpx 0rpx;
  }
  .title {
    height: 60rpx;
    line-height: 60rpx;
    color: #333;
    font-size: 28rpx;
    border-bottom: 1rpx solid #f7f7f8;
    .more {
      float: right;
      padding-left: 20rpx;
      font-size: 24rpx;
      color: #999;
    }
  }
  .section {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    padding: 20rpx 0;
    .goods {
      width: 150rpx;
      margin: 0rpx 30rpx 20rpx 0;
      &:nth-child(3n) {
        margin-right: 0;
      }
      image {
        width: 150rpx;
        height: 150rpx;
      }
      .name {
        padding: 5rpx;
        font-size: 22rpx;
        color: #333;
      }
      .price {
        padding: 5rpx;
        font-size: 18rpx;
        color: #cf4444;
      }
      .number {
        font-size: 24rpx;
        margin-left: 2rpx;
      }
    }
  }
}
</style>
